<?php
/**
 * Controller_Article
 *
 * Класс Controller_Article отвечает за обработку страниц статей
 *
 * @author Mike
 */
class Controller_Article extends Controller_Base {

	/**
	 * Выбор действия по URL
	 *
	 */
	public function action() {
		$this->addCrumb('Главная', W_ABSPATH);
		$this->addCrumb('Статьи', W_ABSPATH . 'articles');
		switch ($this->arrRequest[1]) {
			case '':
				$this->actionArticlesPage();
				break;
			default:
				$objArticleCategory = new ArticleCategory();
				$objArticleCategory->find('first', array('conditions' => "`category_url` ='" . Service_Escape::absolutelyEscapeString($this->arrRequest[1]) . "'"));
				if(!$objArticleCategory->id) throw new Exception('', 404);
				
				if(empty($this->arrRequest[2])) $this->actionArticleCategoryPage($objArticleCategory);
				else {
					$objArticle = new Article();
					$objArticle->find('first', array('conditions' => "`url` ='" . Service_Escape::absolutelyEscapeString($this->arrRequest[2]) . "' AND `article`.`essence_type` = 2"));
					if(!$objArticle->id) throw new Exception('', 404);
					
					$this->actionArticlePage($objArticle, $objArticleCategory);
				}
		}
	}

	/**
	 * Страница всех статей
	 *
	 */
	public function actionArticlesPage() {
		$objArticle = new Article();
		$arrArticles = $objArticle->find('all', array(
			'select' => '`article`.*, `article_category`.`category_url`',
			'conditions' => '`article`.`essence_type` = 2',
			'joins' => 'LEFT JOIN `article_category` USING(`ID_article_category`)',
			'order' => '`created_at` DESC',
			'limit' => 20
		));
		
		$this->title = $this->pageHead = 'Последние 20 статей';
		$this->showHeader();
			View_Article::showArticlesPage($arrArticles);
		$this->showFooter();
	}
	
	/**
	 * Страница информационной статьи
	 *
	 * @param Article $objArticle - объект статьи
	 */
	public function actionArticlePage(Article $objArticle, ArticleCategory $objArticleCategory) {
		if (!isset($_COOKIE['art_inf' . $objArticle->id])) { // Если для этой статьи нет куки, то наращиваем её счётчик просмотров, и записываем куку
			$currentViews = $objArticle->getField('views');
			setcookie('art_inf' . $objArticle->id, 1, time() + 99999999, W_ABSPATH);
			$objArticle->updateFields(array('views' => $currentViews + 1));
			$objArticle->setField('views', $currentViews + 1);
		}
		
		$this->addCrumb($objArticleCategory->getField('category_name'), W_ABSPATH . 'articles/' . $objArticleCategory->getField('category_url'));
		$this->addCrumb($objArticle->getField('title'), W_ABSPATH . 'articles/' . $objArticle->getField('url'), true);
		$this->bodyAttr = 'info-article';
		$this->keywords = $objArticle->getField('keywords');
		$this->description = $objArticle->getField('description');
		$this->title = $objArticle->getField('title');
		$this->pageHead = $objArticle->getField('article_name') . (User_Seance::isAdmin() ? ' (<a href="' . W_ADMIN . 'articles/edit/' . $objArticle->id . '">Редактировать</a>)' : '');
				
		$this->showHeader();
			View_Article::showInfoArticlePage($objArticle);
		$this->showFooter();
	}
	
	/**
	 * Страница статей определенной категории
	 * 
	 * @param ArticleCategory $objArticleCategory - объект категории статей
	 */
	public function actionArticleCategoryPage(ArticleCategory $objArticleCategory) {
		$this->addCrumb($objArticleCategory->getField('category_name'), W_ABSPATH . 'articles/' . $objArticleCategory->getField('category_url'), true);
		$objArticle = new Article();
		$arrArticles = $objArticle->find('all', array(
			'select' => '`article`.*, `article_category`.`category_url`',
			'conditions' => "`essence_type` = 2 AND `ID_article_category` = '" . $objArticleCategory->id . "'",
			'order' => '`article`.`created_at` DESC',
			'joins' => 'LEFT JOIN `article_category` USING(`ID_article_category`)'
		));
		
		$this->keywords = $objArticleCategory->getField('meta_keywords');
		$this->description = $objArticleCategory->getField('meta_description');
		$this->title = $objArticleCategory->getField('title');
		$this->pageHead = $objArticleCategory->getField('category_name') . (User_Seance::isAdmin() ? ' (<a href="' . W_ADMIN . 'article_category/edit/' . $objArticleCategory->id . '">Редактировать</a>)' : '');
		
		$this->showHeader();
			View_ArticleCategory::showCategoryPage($arrArticles);
		$this->showFooter();
	}
	
	/**
	 * Левый столбец
	 */
	protected function showLeft() {
		$objArticleCategory = new ArticleCategory();
		$arrArticleCategories = $objArticleCategory->find('all');
	
		View_Article::showLeft($arrArticleCategories);
	}
}